package org.domain.demo.session.user;

import javax.persistence.EntityManager;

import org.jboss.seam.international.StatusMessages;
import org.jboss.seam.international.StatusMessage.Severity;

import org.domain.demo.dao.UsersDAO;
import org.domain.demo.entity.Users;
import org.h2.util.StringUtils;

public class UserValidator {
	
	private StatusMessages statusMessages;
	
	private UsersDAO usersDAO;
	
	public UserValidator(StatusMessages statusMessages,UsersDAO usersDAO){
		this.statusMessages = statusMessages;
		this.usersDAO = usersDAO;
	}
	
	public boolean valitateUser(EntityManager entityManager,Users user){
		boolean status = true;
		if(StringUtils.isNullOrEmpty(user.getFirstName())){
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.nullFirstName");
			status = false;
		}
		if(StringUtils.isNullOrEmpty(user.getLastName())){
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.nullLastName");
			status = false;
		}
		if(StringUtils.isNullOrEmpty(user.getPrincipal())){
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.nullPrincipal");
			status = false;
		}
		if(StringUtils.isNullOrEmpty(user.getPassword())){
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.nullPassword");
			status = false;
		}
		if(StringUtils.isNullOrEmpty(user.getRepeatPassword())){
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.nullRepeatPassword");
			status = false;
		}
		if(!user.getPassword().equals(user.getRepeatPassword()))
		{
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.repeatPassword");
			status = false;
		}
		if(!StringUtils.isNullOrEmpty(user.getPrincipal()) && 
				usersDAO.findUsersByUserName(user.getPrincipal(), entityManager) != null)
		{
			statusMessages.addFromResourceBundle(Severity.ERROR,"user.save.nameExist",user.getPrincipal());
			status = false;
		}
		return status;
	}

}
